package com.example.controller;


import com.example.Dao.EmployeeMapper;
import com.example.pojo.Department;
import com.example.pojo.Employee;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class EmployeeController {
    @Autowired
    EmployeeMapper employeeMapper;


    @GetMapping("/emps")
    public  String selectAll(Model model){
        //查询所有部门的信息
        List<Department> departments=employeeMapper.selectDepart();
        List<Employee> employees=employeeMapper.selectAll();
        model.addAttribute("departments",departments);
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAddpage(Model model){
        //查询所有部门的信息
        List<Department> departments=employeeMapper.selectDepart();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    @PostMapping("/emp")
    public String doAdd(Employee employee){
        //执行保存操作
        employeeMapper.save(employee);
        return "redirect:/emps";
    }
    @GetMapping("/emp/{id}")
    public String goupdate(@PathVariable("id")Integer id, Model model){
        //查询数据
        List<Department> departments=employeeMapper.selectDepart();
        Employee employee = employeeMapper.selectById(id);
        System.out.println(employee);
        model.addAttribute("departments",departments);
        model.addAttribute("employee",employee);
        return "emp/update";
    }
    @PostMapping("/doupdate")
    public String doupdate(Employee employee){
        //执行更新操作
        employeeMapper.update(employee);
        return "redirect:/emps";
    }
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id")Integer id){
        //执行删除操作
        employeeMapper.delete(id);
        return "redirect:/emps";
    }



}
